package com.shujia.flink.transform;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

public class Demo02FlatMap {
    public static void main(String[] args) throws Exception {
        /*
         * flatMap：传入一条数据返回N条
         */

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        ArrayList<String> arr = new ArrayList<>();
        arr.add("java,scala,python");
        arr.add("java");

        DataStream<String> arrDS = env.fromCollection(arr);

        arrDS.flatMap(new FlatMapFunction<String, String>() {
            /**
             * 传入一条数据flatMap方法就会执行一次
             */
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                for (String word : value.split(",")) {
                    out.collect(word);
                }
            }
        }).print();


        env.execute();
    }
}
